#!/bin/bash

TOP=`pwd`
PATCH_DIR=patches
#$1 means the first parameter of sh command.
TARGET_TAG=$1
#PATCHES_NAME=PATCHES_`date|sed "s/ /_/g"|sed "s/:/-/g"`
#CODES_NAME=CODES_`date|sed "s/ /_/g"|sed "s/:/-/g"`
PATCHES_NAME=PATCHES
#PATCHES_NAME=CODES
PROJECT_CHECK=libcore

#check the first parameter
if [ ! -n "$1" ]; then
	echo "please input tag name for building patches"
	echo "e.g. ./build-patches.sh intel-alpha2"
	exit
fi

set -e
#verify tag name user inputed is available.
cd ${PROJECT_CHECK}
git show $1 1>/dev/null 
cd ${TOP}
set +e

rm -rf ${PATCHES_NAME}.tar


#compress some projects added or moved after intel-alpha2
echo ""
echo "compress some projects added or moved after intel-alpha2 ... ... "
sleep 2
tar -rvf ${PATCHES_NAME}.tar hardware/windriver/include --exclude=.git 
tar -rvf ${PATCHES_NAME}.tar hardware/windriver/libalslightness --exclude=.git 
tar -rvf ${PATCHES_NAME}.tar hardware/windriver/libgps --exclude=.git  
tar -rvf ${PATCHES_NAME}.tar hardware/windriver/wifi_bt --exclude=.git 
tar -rvf ${PATCHES_NAME}.tar hardware/windriver/dmidecode --exclude=.git 
tar -rvf ${PATCHES_NAME}.tar hardware/windriver/SMBIOS --exclude=.git 

tar -rvf ${PATCHES_NAME}.tar hardware/intel/PRIVATE/release --exclude=.git

# this file does be useless and redundance, but I don't miss it anyway. 
tar -rvf ${PATCHES_NAME}.tar hardware/intel/PRIVATE/tiwl1283/WiLink/CUDK/os/linux/src/wpa_ctrl.c

# compress projects which can not been merged with patches.
#tar -rvf ${PATCHES_NAME}.tar hardware/intel/psb_headers --exclude=.git  

echo ""
echo "build patches for each projects ... "

# if patches folder exist , REMOVE it directly.
NUM=`ls -1a ${TOP}/$PROJECT_PATH/${PATCH_DIR} 2>/dev/null | wc -l`
if [ ${NUM} -gt 0 ]; then
	echo "WARNING: \"patches\" folder exist Remove it ... "
	rm -rf ${TOP}/$PROJECT_PATH/${PATCH_DIR}
fi

# create patches directory
mkdir $TOP/patches

#repo forall -c "git format-patch ${TARGET_TAG} 2>/dev/null"
repo forall -c "${TOP}/create-patches.sh $1 ${TOP} ${PATCHES_NAME}"

#if copying patches file into "patches" folder, it cost much times, so don't do that.
if [ 1 -eq 0 ]; then

	#put all of patches into folder named with "patches"
	#if patches folder exist remove it directly
	NUM=`ls -1a patches 2>/dev/null | wc -l`
	if [ ${NUM} -gt 0 ]; then
		echo "WARNING: \"patches\" folder exist , remove it!"
		rm -rf ${PATCH_DIR}
	fi
	
	#create folder named with "patches" for patch files
	mkdir ${PATCH_DIR}
	
	#copy patch files into "patches" folder
	echo "searching patches for each project ... "
	for DIR in `find . -type d` ; do	
		pushd ${DIR} > /dev/null
		NUM=`ls -1 *.patch 2> /dev/null | wc -l `
		if [ "${NUM}" != "0" ]; then
			echo "copying patches in ${DIR} ... "
			mkdir -p ${TOP}/${PATCH_DIR}/`echo ${DIR}|sed "s/^.\///"`
			cp *.patch ${TOP}/${PATCH_DIR}/`echo ${DIR}|sed "s/^.\///"`
		fi
		popd > /dev/null
	done
fi

set -e
echo ""
echo "pack patches file into tar file ... "
#find . -name "*.patch" | sed "s/^\.\///" | xargs -i tar -rvf ${PATCHES_NAME}.tar {}
find $PATCH_DIR -name "*.patch" | sed "s/^\.\///" | xargs -i tar -rvf ${PATCHES_NAME}.tar {}

echo ""
echo "build all patches sucessfully."
